<script>
import { requestGet } from '@/api/system/common'
import { delCollection, saveCollection } from '@/api/index/service/entity'

export default {
  name: 'OfficeHallV2',
  props: {
    temp: {
      type: Object,
      default() {
        return {}
      }
    },
    showHeader: {
      type: Boolean,
      default: true
    },
    layout: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      baseURL: process.env.VUE_APP_BASE_API.substring(0, process.env.VUE_APP_BASE_API.length - 1),
      query: {
        blurry: '',
        zxpt: 2,
        fwfl: '1565760539328544'
      },
      queryList: [],
      column: [],
      dataList: [],
      parentList: [],
      allFwfl: [],
      allTmp: [],
      active: '',
      customStyle: {},
      titleStyle: {},
      bodyStyle: {}
    }
  },
  // 1565760539328544
  computed: {
    borderStyle() {
      if (this.temp.sfdzqp === true) {
        return { position: 'absolute', left: '0px', right: '0px', top: '0px', bottom: '0px' }
      }
      return { height: this.temp.defaultHeight }
    }
  },
  watch: {
    layout: {
      handler(newData, oldData) {
        this.customStyle = newData
        this.titleStyle = { backgroundColor: newData.mrlb, opacity: newData.tmd }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    },
    temp: {
      handler(newData, oldData) {
        if (this.showHeader) {
          const pzx = JSON.parse(newData.data.pzx)
          this.column = pzx
          this.query.fwfl = this.column.fwdl
        } else {
          const pzx = JSON.parse(newData.pzx)
          this.column = pzx
          this.query.fwfl = this.column.fwdl
        }
        if (undefined !== this.column.query && this.column.query !== null) {
          this.queryList = this.column.query
        } else {
          this.queryList = []
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  created() {
    this.initAllFw()
  },
  methods: {
    initAllFw() {
      this.query.fwfl = this.temp.fwfl
      requestGet('rest/api/s/service/list/pc/fwfl/all', this.query).then((res) => {
        const { data } = res
        this.dataList = data
        this.parentList = []
        this.allFwfl = []
        if (undefined !== data && data !== null && data.length > 0) {
          for (let i = 0; i < data.length; i++) {
            this.parentList.push(data[i])
            if (undefined !== data[i].dataList && data[i].dataList !== null && data[i].dataList.length > 0) {
              for (let j = 0; j < data[i].dataList.length; j++) {
                this.allFwfl.push(data[i].dataList[j])
              }
            }
          }
          this.allTmp = this.allFwfl
        } else {
          this.allTmp = []
        }
      })
    },
    openEntity(data) { // 打开详情页
      if (this.column.isOpenEntity) { // 允许打开详情页
        const params = []
        let url = ''
        if (undefined !== this.column.entityParams && this.column.entityParams !== null && this.column.entityParams.length > 0) {
          this.column.entityParams.forEach(item => {
            const obj = { code: item.code, value: data[item.value] }
            params.push(obj)
          })
        }
        if (this.column.entityOpenType === 1) { // 地址参数的方式打开
          url = data[this.column.entityUrl]
        }
        if (this.column.entityOpenType === 2) { // 地址传参查询
          if (undefined !== this.column.params && this.column.params !== null && this.column.params.length > 0) {
            url = this.column.entityUrl
            this.column.params.forEach(item => {
              url = url.replace(new RegExp('\\$\\{([\\s]*?)(' + item.code + '{1})([\\s]*?)([^}])*\\}'), data[item.column])
            })
          }
        }
        const object = { title: data.name, params: params, setting: this.temp, entityTemp: this.column.entityTemplateId, temp: this.column.entityTemp, openType: this.column.entityOpenType, url: url }
        this.$emit('fetch-entity', object)
      }
    },
    scfw(data) {
      // 收藏服务
      this.loadings = true
      saveCollection({ serviceId: data.id }).then(res => {
        if (res.state === 200) {
          data.sfsc = true
          this.$notify({
            title: '收藏成功',
            message: '服务收藏成功！',
            type: 'success'
          })
          this.loadings = false
        } else {
          this.$notify({
            title: '收藏失败！',
            message: res.msg,
            type: 'warning'
          })
          this.loadings = false
        }
      })
    },
    qxsc(data) {
      // 取消收藏
      this.loadings = true
      delCollection({ serviceId: data.id }).then(res => {
        if (res.state === 200) {
          data.sfsc = false
          this.$notify({
            title: '取消成功',
            message: '服务取消收藏成功！',
            type: 'success'
          })
          this.loadings = false
        } else {
          this.$notify({
            title: '取消失败！',
            message: res.msg,
            type: 'warning'
          })
          this.loadings = false
        }
      })
    },
    changeVal(val) {
      this.active = val + ''
      if (this.active === '') {
        this.allTmp = this.allFwfl
      } else {
        this.allTmp = []
        for (let i = 0; i < this.allFwfl.length; i++) {
          if (this.allFwfl[i].fwflid === this.active) {
            this.allTmp.push(this.allFwfl[i])
          }
        }
      }
    }
  }
}
</script>

<template>
  <div class="custom-office-hall">
    <div class="office-hall-header">
      <div class="office-header-query">
        <input v-model="query.blurry" placeholder="请输入要查询查询的服务名称">
        <div class="office-query" @click="initAllFw">
          <i class="el-icon-search" />
        </div>
      </div>
    </div>
    <div class="office-hall-content">
      <div class="office-hall-left">
        <div class="office-hall-li" :class="active === '' ? 'active' : ''" @click="changeVal('')">
          <div class="office-hall-icon">
            <i class="el-icon-office-building" />
          </div>
          <div class="office-hall-text">全部服务</div>
        </div>
        <div v-for="(item, index) in parentList" :key="'p-'+index" :class="item.flid === active ? 'active' : ''" class="office-hall-li" @click="changeVal(item.flid)">
          <div class="office-hall-icon">
            <i class="el-icon-office-building" />
          </div>
          <div class="office-hall-text">{{ item.flmc }}</div>
        </div>
      </div>
      <div class="office-hall-right">
        <div v-for="(item, index) in allTmp" :key="'u-'+index" class="office-hall-ul" @click="openEntity(item)">
          <div class="office-hall-body">
            <div class="office-hall-image">
              <el-image lazy :src="baseURL+item.appIcon" />
            </div>
            <div class="office-hall-body-right">
              <div class="office-hall-office-li">{{ item.name }}
                <i v-if="item.sfsc === true" class="el-icon-star-on" />
                <i v-if="item.sfsc === false" class="el-icon-star-off" />
              </div>
              <div class="office-hall-office-li office-font">
                <div class="office-hall-title">共（<span style="color: red">{{ item.fwcs }}</span>）人访问</div>
                <div class="office-hall-title">
                  <el-button v-if="item.sfsc === false" type="primary" size="mini" round @click.stop="scfw(item)">开始订阅</el-button>
                  <el-button v-else type="warning" size="mini" round @click.stop="qxsc(item)">取消订阅</el-button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.custom-office-hall {
  width: 100%;
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  overflow: hidden;

  .office-hall-header {
    width: 100%;
    height: 50px;
    padding: 10px 40px;
    box-sizing: border-box;
    border-bottom: 1px solid #efefef;

    .office-header-query {
      width: 100%;
      height: 100%;
      background-color: #efefef;
      border-radius: 20px;
      padding: 0 30px;
      font-size: 0.9rem;
      position: relative;

      input {
        background: none;
        outline: none;
        border: 0;
        width: 100%;
        height: 30px;
      }

      .office-query {
        width: 30px;
        height: 30px;
        text-align: center;
        line-height: 30px;
        cursor: pointer;
        color: #606266;
        position: absolute;
        right: 7px;
        top: 0;
      }
    }
  }

  .office-hall-content {
    width: 100%;
    height: 100%;
    position: relative;
    display: flex;
    flex-flow: row nowrap;

    .office-hall-left {
      width: 100px;
      overflow-y: auto;
      background-color: #e9e9eb;

      .office-hall-li {
        width: 100%;
        height: 35px;
        display: flex;
        align-items: center;

        .office-hall-icon {
          width: 20px;
          height: 35px;
          line-height: 35px;
          padding-left: 5px;
        }

        .office-hall-text {
          flex: 1 1;
          height: 35px;
          line-height: 35px;
          padding-left: 5px;
          font-size: 0.8rem;
        }
      }

      .office-hall-li.active {
        background-color: #ffffff;
        .office-hall-icon {
          color: #409eff;
        }
      }
    }

    .office-hall-right {
      flex: 1 1;
      height: 100%;
      overflow-y: auto;
      background-color: #f4f4f5;

      .office-hall-ul {
        width: 100%;
        height: 80px;
        padding: 10px 10px 0 10px;
        box-sizing: border-box;

        .office-hall-body {
          width: 100%;
          height: 100%;
          background-color: #ffffff;
          padding: 10px;
          box-sizing: border-box;
          border-radius: 5px;
          box-shadow: 0 2px 4px rgba(0, 0, 0, .12);
          display: flex;
          flex-flow: row nowrap;

          .office-hall-image {
            width: 50px;
            height: 50px;
            background-color: #efefef;
          }

          .office-hall-body-right {
            flex: 1 1;
            padding-left: 10px;
            box-sizing: border-box;
            display: flex;
            flex-flow: column nowrap;
            justify-content: space-between;

            .office-hall-office-li {
              width: 100%;
              display: flex;
              font-size: 0.9rem;
              flex-flow: row nowrap;
              align-items: center;
              i {
                padding-left: 5px;
                color: #F56C6C;
                font-size: 1.1rem;
              }
            }

            .office-hall-office-li.office-font {
              color: #606266;
              font-size: 0.9rem;
              justify-content: space-between;
            }
          }
        }
      }
    }
  }
}
</style>
